package university;

import university.exceptions.*;

import java.io.Serializable;
import java.util.*;

/**
 * Abstract class to describe basic info about person
 * @author anatolii.bondarenko
 */

class People  implements Serializable {
    private String firstName;
    private String lastName;
    transient private Date birthDate;


    /**
     * This class describes general information about personal in university
     * @param fn is person's first name
     * @param ln is person's last name
     * @param year is person's year of birth
     * @param month is person's month of birth
     * @param day is person's day of birth
     */

    public People(String fn, String ln, int year, int month, int day) throws InvalidDateException, EmptyStringException {
		firstName = fn;
		if (fn.trim() == ""){
            throw new EmptyStringException("First name cannot be empty or contain only whitespaces");
        }
        lastName = ln;
        if (ln.trim() == ""){
            throw new EmptyStringException("Last name cannot be empty or contain only whitespaces");
        }

		Calendar now = new GregorianCalendar().getInstance();
        if (year < now.get(Calendar.YEAR) - 100 || year > now.get(Calendar.YEAR) - 18){
            throw new InvalidBirthDateException("Invalid year of birth");
        }
        if (month < Calendar.JANUARY || month > Calendar.DECEMBER){
            throw new InvalidDateException("Invalid Month");
        }
        if (day < 1){
            throw new InvalidDateException("Invalid Day");
        }
        if(month == Calendar.FEBRUARY && (year % 4 == 0) && day > 29){
            throw new InvalidDateException("Invalid Day");
        }
        if(month == Calendar.FEBRUARY && (year % 4 != 0) && day > 28){
            throw new InvalidDateException("Invalid Day");
        }
        if((month == Calendar.APRIL || month == Calendar.JUNE || month == Calendar.SEPTEMBER || month == Calendar.NOVEMBER) && day > 30){
            throw new InvalidDateException("Invalid Day");
        }
        if ((month == Calendar.JANUARY || month == Calendar.MARCH || month == Calendar.MAY || month == Calendar.JULY || month == Calendar.AUGUST || month == Calendar.OCTOBER || month == Calendar.DECEMBER) && day > 31){
            throw new InvalidDateException("Invalid Day");
        }
        GregorianCalendar calendar = new GregorianCalendar(year, month, day);
		birthDate = calendar.getTime();
	}

    /**
     * Get person's name
     * @return person's name (first name and last name)
     */
	public String getName(){
		return firstName + " " + lastName;
	}

    /**
     * Get person's birth date
     * @return person's birth date
     */
	public Date getBirthDate(){
		return birthDate;
	}
		

}
